package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcComment;
import com.example.benefitcommunity.dao.BcCommentDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcCommentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(BcComment)表服务实现类
 *
 * @author makejava
 * @since 2025-08-27 16:40:21
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcCommentService")
public class BcCommentServiceImpl implements BcCommentService {
    @Resource
    private BcCommentDao bcCommentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public List<BcComment> queryById(Integer id) {
        return this.bcCommentDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcComment   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcComment> queryByPage(BcComment bcComment, PageRequest pageRequest) {
        long total = this.bcCommentDao.count(bcComment);
        return new PageImpl<>(this.bcCommentDao.queryAllByLimit(bcComment, pageRequest), pageRequest, total);
    }

    /**
     * @param dynamicid 动态id
     * @return 查询结果
     */
    @Override
    public Result<Object> queryByComment(Integer dynamicid) {
        BcComment bcComment = new BcComment();
        bcComment.setDynamicid(dynamicid);
        List<BcComment> list = this.bcCommentDao.queryAll(bcComment);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), list);
    }

    /**
     * 新增数据
     *
     * @param bcComment 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcComment bcComment) {
        if (this.bcCommentDao.insert(bcComment) > 0) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "发表动态新增成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "发表动态新增失败！");
    }

    /**
     * 修改数据
     *
     * @param bcComment 实例对象
     * @return 实例对象
     */
    @Override
    public BcComment update(BcComment bcComment) {
        this.bcCommentDao.update(bcComment);
        this.queryById(bcComment.getDynamicid());
        return bcComment;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bcCommentDao.deleteById() > 0;
    }
}
